
package com.cms.customer.handler;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.vinote.smart.platform.comm.PlatformConst;
import net.vinote.smart.platform.exception.SerialNoException;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.Field;
import net.vinote.smart.platform.module.database.PrimaryKeyGenerateStrategy;
import net.vinote.smart.platform.module.database.SerialNoManager;
import net.vinote.smart.platform.module.database.Table;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.module.database.bo.DBQuery;
import net.vinote.smart.platform.service.SRE;


public class CustomerInfoKeyStrategy implements PrimaryKeyGenerateStrategy
{

	private BizObject bo;

	private String tableName;

	private Map<String, Integer> serialMap = new HashMap<String, Integer>();

	private DecimalFormat format = new DecimalFormat("0000000");

	@Override
	public void setBizObject(BizObject bo)
	{
		this.bo = bo;
	}

	@Override
	public void setTableName(String tableName)
	{
		this.tableName = tableName;
	}

	@Override
	public synchronized String getSerialNo()
	{
		String customerSource = bo.getAttribute("CUSTOMERSOURCE");
		String prefix;
		// 广交会
		if ("gjh".equals(customerSource))
		{
			prefix = "GJH";
		}
		// 华交会
		else if ("hjh".equals(customerSource))
		{
			prefix = "HJH";
		}
		// 市场推广
		else if ("sctg".equals(customerSource))
		{
			prefix = "SCTG";
		}
		// 阿里巴巴网站
		else if ("albb".equals(customerSource))
		{
			prefix = "ALBB";
		}
		// 未指定则采用默认策略
		else
		{
			return SerialNoManager.getInstance().getSerialNo(tableName);
		}
		Integer index = serialMap.get(prefix);
		if (index == null)
		{
			// 查询数据库中最大值
			Table tab = SRE.getTable(tableName);
			List<Field> keyList = tab.getKey();
			if (keyList.size() != PlatformConst.Number.ONE)
			{
				throw new SerialNoException("primary key number is not one!");
			}
			// 获取当前表的最新serialNo
			DBManager bm = new DBManager(tableName);
			try
			{
				String key = keyList.get(0).getFieldName();
				DBQuery query = bm.createQuery(new String[]
				{
					key
				}, key + " like ? order by " + key + " desc").setParamVal(
						prefix + "%");
				query.setPagination(1, 1);// 提高性能
				BizObject bo = query.getSingleResult();
				if (bo == null)
				{
					index = 0;
				}
				else
				{
					index = format.parse(
							bo.getAttribute(key).substring(prefix.length()))
							.intValue();
				}
				serialMap.put(prefix, index);
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
			catch (ParseException e)
			{
				e.printStackTrace();
			}
		}
		index = serialMap.get(prefix) + 1;
		serialMap.put(prefix, index);
		return prefix + format.format(index);
	}

}
